package com.abbas.abchat.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.abchat.R;
import com.abbas.abchat.activity.BaseActivity;
import com.abbas.abchat.dao.AccountDao;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.entity.FriendRound;
import com.abbas.abchat.utils.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/20 16:45
 */
public class FriendRoundAdapter extends BaseAdapter {

    private Context context;
    private AccountDao accountDao;
    private List<FriendRound> friendRounds;

    public FriendRoundAdapter(Context context, AccountDao accountDao, List<FriendRound> friendRounds) {
        this.context = context;
        this.accountDao = accountDao;
        this.friendRounds = friendRounds;
    }

    public void setFriendRounds(List<FriendRound> friendRounds) {
        this.friendRounds = friendRounds;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return friendRounds.size();
    }

    @Override
    public Object getItem(int position) {
        return friendRounds.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.friend_round_item,parent,false);
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        FriendRound friendRound = friendRounds.get(position);
        Account account = null;
        try {
            account = accountDao.findById(friendRounds.get(position).getpId());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (account != null){
            String imgPath = account.getImgPath();
            if (!TextUtils.isEmpty(imgPath)){
                Glide.with(context)
                        .load(imgPath)
                        .transform(new RoundedCorners(18))
                        .into(viewHolder.im);
            }
            viewHolder.name.setText(String.valueOf(account.getName()));
        }

        viewHolder.content.setText(friendRound.getContent());
        viewHolder.time.setText(TimeUtils.getInstance(friendRound.getPubDate()).compareOfChatTag());

        return convertView;
    }


    static class ViewHolder{
        ImageView im;
        TextView name;
        TextView content;
        TextView time;
    }
}
